package day14;
/*
* 异常抛出
* */
public class Demo2 {
    public static void main(String[] args) {
        Student student = new Student("张三",18);

        Integer i = 10;

        System.out.println(student.equals(i));
    }
}

class Student{

    private String name;

    private int age;

    //构造方法
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    //重写equals方法，判断两个对象是否相等
    @Override
    public boolean equals(Object obj){
        //比较两个对象的地址
        if(this == obj){
            return true;
        }

        //判断Obj是否是Student对象
        if(obj instanceof Student){
            Student student = (Student) obj;

            return student.age == this.age && this.name.equals(student.name);

        }else{
            //如果程序由于参数或其他问题导致当前逻辑无法执行，则可以
            //抛出某个异常，携带异常信息告知给当前方法的调用者
            throw new ClassCastException("你传的是什么类型？我需要Student类型");
        }

    }
}